package com.hepinghospital.user.ui.kotlin

import android.annotation.SuppressLint
import android.graphics.Rect
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import com.hepinghospital.user.R
import com.hepinghospital.user.base.BaseActivity
import com.hepinghospital.user.ui.dialog.ConfirmDialog
import com.hepinghospital.user.ui.dialog.RulesDialogFragment
import com.hepinghospital.user.util.GlideUtil
import com.hepinghospital.user.viewmodel.DailyRecordListViewModel
import jz.joyoung.robot.util.DpUtil
import kotlinx.android.synthetic.main.activity_daily_record.*
import kotlinx.android.synthetic.main.include_empty.*
import kotlinx.android.synthetic.main.include_title.*

/**
 * 挂号记录
 */

class DailyRecordListActivity : BaseActivity() {

    private lateinit var mAdapter: MyAdapter
    val viewModel by lazy { ViewModelProviders.of(this).get(DailyRecordListViewModel::class.java) }

    override fun initViews() {
        tv_title.text = "挂号记录"
        val layoutManager = LinearLayoutManager(this)
        mAdapter = MyAdapter()
        recyclerView.layoutManager = layoutManager
        recyclerView.adapter = mAdapter
        recyclerView.addItemDecoration(object : ItemDecoration() {
            override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
                super.getItemOffsets(outRect, view, parent, state)
                outRect.bottom = DpUtil.dip2px(this@DailyRecordListActivity, 16f)
            }
        })

        refreshLayout.isEnableLoadMore = false
        refreshLayout.setOnRefreshListener { initData() }
        refreshLayout.setOnLoadMoreListener { }


        viewModel.dataChanged.observe(this, Observer {
            mAdapter.notifyDataSetChanged()
            refreshLayout.finishRefresh(true)
            if (viewModel.list.isEmpty()) {
                refreshLayout.visibility = View.GONE
                cl_empty.visibility = View.VISIBLE
            } else {
                refreshLayout.visibility = View.VISIBLE
                cl_empty.visibility = View.GONE
            }
        })
        viewModel.refreshFail.observe(this, Observer {
            refreshLayout.finishRefresh(false)
        })

        btn_must_know.setOnClickListener {
            showRulesDialogFragment()
        }
    }

    private lateinit var mRulesDialogFragment: RulesDialogFragment

    private fun showRulesDialogFragment() {
        mRulesDialogFragment = RulesDialogFragment.newInstance("8")
        mRulesDialogFragment.setmListener { mRulesDialogFragment.dismiss() }
        mRulesDialogFragment.show(supportFragmentManager, "mSchudeleDialogFragment")
    }

    override fun initData() {
        viewModel.getDailyList()
    }

    override fun getLayoutResource(): Int {
        return R.layout.activity_daily_record
    }


    inner class MyAdapter : RecyclerView.Adapter<MyAdapter.Holder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
            return Holder(
                LayoutInflater.from(this@DailyRecordListActivity)
                    .inflate(R.layout.item_daily_record, parent, false)
            )
        }

        override fun getItemCount(): Int {
            return viewModel.list.size
        }

        @SuppressLint("SetTextI18n")
        override fun onBindViewHolder(holder: Holder, position: Int) {
            holder.tv_name.text = viewModel.list[position].docName
            holder.tv_patient.text = "${viewModel.list[position].patientName}"
            holder.tv_dep_name.text = viewModel.list[position].deptName
            holder.tv_time.text = "${viewModel.list[position].scheduleDate}  ${viewModel.list[position].scheduleTime}"
            holder.tv_status.text = viewModel.list[position].flagName

            when (viewModel.list[position].flag) {
                401 -> {//已预约
                    holder.btn_cancel.visibility = View.VISIBLE
                    holder.tv_pay.visibility = View.GONE
                    holder.tv_status.setTextColor(ContextCompat.getColor(mContext, R.color.mainBlue))
//                    holder.tv_dep_name.setTextColor(ContextCompat.getColor(mContext, R.color.mainGreen))
//                    holder.tv_dep_name.setBackgroundResource(R.drawable.shape_f2f9f8_9dp)
//                    holder.tv_name.setTextColor(ContextCompat.getColor(mContext, R.color.ff333333))
//                    holder.tv_patient.setTextColor(ContextCompat.getColor(mContext, R.color.ff999999))
//                    holder.tv_time.setTextColor(ContextCompat.getColor(mContext, R.color.ff999999))
                }
                402 -> {//已取消
                    holder.btn_cancel.visibility = View.GONE
                    holder.tv_pay.visibility = View.GONE
                    holder.tv_status.setTextColor(ContextCompat.getColor(mContext, R.color.ffcccccc))
//                    holder.tv_dep_name.setTextColor(ContextCompat.getColor(mContext, R.color.ffcccccc))
//                    holder.tv_dep_name.setBackgroundResource(R.drawable.shape_f8f8f8_9dp)
//                    holder.tv_name.setTextColor(ContextCompat.getColor(mContext, R.color.ffcccccc))
//                    holder.tv_patient.setTextColor(ContextCompat.getColor(mContext, R.color.ffcccccc))
//                    holder.tv_time.setTextColor(ContextCompat.getColor(mContext, R.color.ffcccccc))
                }
                403 -> {//已过期
                    holder.btn_cancel.visibility = View.GONE
                    holder.tv_pay.visibility = View.GONE
                    holder.tv_status.setTextColor(ContextCompat.getColor(mContext, R.color.ff999999))
//                    holder.tv_dep_name.setTextColor(ContextCompat.getColor(mContext, R.color.ffcccccc))
//                    holder.tv_dep_name.setBackgroundResource(R.drawable.shape_f8f8f8_9dp)
//                    holder.tv_name.setTextColor(ContextCompat.getColor(mContext, R.color.ff999999))
//                    holder.tv_patient.setTextColor(ContextCompat.getColor(mContext, R.color.ff999999))
//                    holder.tv_time.setTextColor(ContextCompat.getColor(mContext, R.color.ff999999))
                }
            }
            holder.btn_cancel.setOnClickListener {
                showConfirmDialog(viewModel.list[position].outpId)
            }
            holder.itemView.setOnClickListener {
                val bundle = Bundle()
                bundle.putString("outpId", viewModel.list[position].outpId)
                startActivity(DailyDetailActivity::class.java, bundle)
            }
            GlideUtil.loadImage(mContext, "", holder.img_head, R.drawable.ic_head_doctor_woman)
        }

        inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val img_head: ImageView = itemView.findViewById(R.id.img_head)
            val tv_name: TextView = itemView.findViewById(R.id.tv_name)
            val tv_patient: TextView = itemView.findViewById(R.id.tv_patient)
            val tv_time: TextView = itemView.findViewById(R.id.tv_time)
            val tv_dep_name: TextView = itemView.findViewById(R.id.tv_dep_name)
            val tv_status: TextView = itemView.findViewById(R.id.tv_status)
            val btn_cancel: TextView = itemView.findViewById(R.id.btn_cancel)
            val tv_pay: TextView = itemView.findViewById(R.id.tv_pay)

        }
    }

    fun showConfirmDialog(outpId: String) {
        val dialog = ConfirmDialog.newInstance("确定取消预约吗？","")
        dialog.show(supportFragmentManager, "ConfirmDialog")
        dialog.setmListener(object : ConfirmDialog.OnItemClickListener {
            override fun onConfirm() {
                viewModel.cancelDaily(outpId)
            }

            override fun onCancel() {
            }
        })
    }
}